package pe.gob.mtpe.renoccsys.mybatis.implementacion;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import pe.gob.mtpe.renoccsys.bean.ParamConsultaRenocc;
import pe.gob.mtpe.renoccsys.bean.ParamConsultaRetcc;
import pe.gob.mtpe.renoccsys.bean.Renocc;
import pe.gob.mtpe.renoccsys.bean.Usuario;
import pe.gob.mtpe.renoccsys.mybatis.comun.MyBatisConnectionFactory;
import pe.gob.mtpe.renoccsys.mybatis.interfase.INTRenocc;
import pe.gob.mtpe.renoccsys.mybatis.interfase.INTRetcc;

public class IMPRenocc implements INTRenocc {

	private SqlSessionFactory sqlSessionFactory;

	public IMPRenocc() {
		sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
	}

	@Override
	public Usuario obtDatos(String v_ruc) throws Exception {
		System.out.println("BUSCANDO EMPRESA: " + v_ruc);
		Usuario bean = new Usuario();
		ResultSet set = null;
		Map parametros = new HashMap();
		parametros.put("v_ruc", v_ruc);

		SqlSession session = sqlSessionFactory.openSession();
		try {

			bean = (Usuario) session.selectOne("Renocc.obtDatos", parametros);

			if (bean == null) {
				System.out.println("Error nullo o size=0 en obtDatos");
				return null;
			} else {
				return bean;
			}

		} catch (Exception e) {
			System.out.println("---no encontro nada, debe registrar");
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}

	}

	@Override
	public void insertar(Renocc objRenocc) throws Exception {

		SqlSession session = sqlSessionFactory.openSession();

		try {
			session.insert("Renocc.insertar", objRenocc);
			session.commit();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			session.rollback();

		} finally {
			session.close();
		}

	}
	
	public void actualizarPendienteActualizacion(Usuario parametro) throws Exception {

		SqlSession session = sqlSessionFactory.openSession();

		try {
			session.update("Renocc.updatePendienteActualizacion", parametro);			
		} catch (Exception ex) {			
			System.out.println(ex.getMessage());
		} finally {
			session.close();
		}

	}
	 
	public void actualizar(Renocc objRenocc) throws Exception {

		SqlSession session = sqlSessionFactory.openSession();

		try {
			session.update("Renocc.updateRenocc", objRenocc);
			session.commit();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			session.rollback();

		} finally {
			session.close();
		}

	}
	public void updateEstadoXNumreg(ParamConsultaRenocc objRenocc) throws Exception {

		SqlSession session = sqlSessionFactory.openSession();

		try {
			objRenocc.valuestoString();
			session.update("Renocc.updateEstadoXNumreg", objRenocc);
			session.commit();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			session.rollback();

		} finally {
			session.close();
		}

	}
	
	@Override
	public List<Renocc> buscar(ParamConsultaRenocc parametros) throws Exception {

		List<Renocc> lstLRenocc = new ArrayList<Renocc>();
		SqlSession session = sqlSessionFactory.openSession();

		try {
			System.out.println("parametros "+parametros.getnCorrenocc());
			 
			lstLRenocc = session.selectList("Renocc.buscar", parametros);

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		} finally {
			session.close();
		}

		return lstLRenocc;

	}
	public Integer verificaPendienteActualizacion(Usuario parametros) throws Exception {

		Integer   resultado=0;
		SqlSession session = sqlSessionFactory.openSession();

		try {
			System.out.println("parametros "+parametros);
			 
			resultado = session.selectOne("Renocc.verificaPendienteActualizacion", parametros);

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		} finally {
			session.close();
		}

		return resultado;

	}
	public List<Map> mostrarConstancia(ParamConsultaRenocc parametros) throws Exception {

		List<Map> lstLRenocc = new ArrayList<Map>();
		SqlSession session = sqlSessionFactory.openSession();

		try {
			System.out.println("parametros "+parametros.getnCorrenocc());
			 
			lstLRenocc = session.selectList("Renocc.constanciaRenocc", parametros);

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		} finally {
			session.close();
		}

		return lstLRenocc;

	}
	@Override
	public Renocc obtenerPorId(Renocc objRenocc) throws Exception {

		Renocc renoc = null;
		SqlSession session = sqlSessionFactory.openSession();

		try {

			renoc = (Renocc)session.selectOne("Renocc.obtenerPorId", objRenocc);

		} catch (Exception ex) {

		} finally {
			session.close();
		}

		return renoc;

	}

	
	public void culminacionObra(Renocc renocc) throws Exception {
		SqlSession session = sqlSessionFactory.openSession();

		try {
			session.update("Renocc.culminacionObra", renocc);
			session.commit();
			System.out.println("culminacionObra EJECUTADO OK");
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		
	}

}
